package com.nbjtjc.safe.service.emg;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Page;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.mapper.emg.EmgWarehouseMapper;
import com.nbjtjc.safe.model.emg.EmgWarehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class EmgWareHouseService {

    @Autowired
    private EmgWarehouseMapper emgWarehouseMapper;

    @Transactional(rollbackFor = {Throwable.class})
    public Result addOrUpdate(EmgWarehouse emgWarehouse){
        Result result=new Result();
        if(ObjectUtil.isNull(emgWarehouse)) {
            result.setFalse("不能为空");
            return result;
        }
        if(ObjectUtil.isNotNull(emgWarehouse.getId())){
            emgWarehouseMapper.updateByPrimaryKeySelective(emgWarehouse);
        }else{
            emgWarehouseMapper.insertSelective(emgWarehouse);
        }
        result.setData(emgWarehouse);
        return result;
    }


    public Result find(EmgWarehouse emgWarehouse,Page page){
        Result result=new Result();
        PageHelper.startPage(page.getPageCurrent(),page.getPageSize());
        List<EmgWarehouse> emgWarehouses=emgWarehouseMapper.search(emgWarehouse);
        result.setData(new PageInfo<>(emgWarehouses));
        return result;
    }

}
